﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Project.Admin
{
    public partial class CreateTellers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataPOFDataContext db = new DataPOFDataContext();
            string acc = (from x in db.AccountLogins where x.Username == Session["username"].ToString() select x.FullName).FirstOrDefault();
            Label1.Text = acc;
            int ID = (from x in db.AccountLogins select x.Id).Count();
            TextBox1.Text = Convert.ToString(ID + 1);
            Label2.Text = null;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Session["username"] = null;
            Response.Cache.SetNoStore();
            Response.CacheControl = "no-cache";
            Response.Redirect("../Home/Login.aspx");
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            Label2.Text = null;
            DataPOFDataContext data = new DataPOFDataContext();
            int a = (from x in data.AccountLogins where x.Username == TextBox7.Text select x.Id).FirstOrDefault();
            // Add new Customer to data
            if ((TextBox6.Text == "") || (TextBox7.Text == "") || (TextBox2.Text == "") || (TextBox4.Text == "") || (TextBox5.Text == ""))
            { Label2.Text = "Insert data on (*)"; }
            else
                if ( a == 0 )
                {
                    AccountLogin AccountLogin = new AccountLogin();
                    AccountLogin.Id = Convert.ToInt32(TextBox1.Text);
                    AccountLogin.FullName = TextBox6.Text;
                    AccountLogin.Username = TextBox7.Text;
                    AccountLogin.Password = TextBox2.Text;
                    AccountLogin.Email = TextBox4.Text;
                    AccountLogin.Teller_No = TextBox5.Text;
                    AccountLogin.Roll = DropDownList3.SelectedItem.ToString();
                    AccountLogin.LocationID = (from x in data.Locations where x.Location1 == DropDownList2.SelectedValue.ToString() select x.ID).FirstOrDefault();
                    try
                    {

                        data.AccountLogins.InsertOnSubmit(AccountLogin);

                    }

                    catch (Exception ex)
                    {

                        //MessageBox.Show(ex.Message);

                    }

                    finally
                    {

                        // Submit to data

                        data.SubmitChanges();
                        Response.Redirect("../Admin/CreateTellers.aspx");
                    }
                    Label2.Text = "Register successfully !";
                }
                else Label2.Text = "UserName already!";
        }
    }
}
